package jianda;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.HTMLWriter;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

public class Jianda3 {
    public static void main(String[] args) throws IOException {
        // add();
        // update();
        delete();
    }
    public static void add() throws IOException {
        Document doc = null;
        try{
            SAXReader saxReader = new SAXReader();
            doc = saxReader.read("src/jianda/jianda1.xml");
        }catch (Exception e){
            e.printStackTrace();
        }
        // 创建Brand节点
        Element rootElement = doc.getRootElement();
        Element student = DocumentHelper.createElement("student");
        student.addAttribute("id","3");

        Element name = DocumentHelper.createElement("name");
        name.addAttribute("name","王五");

        Element course = DocumentHelper.createElement("course");
        course.addAttribute("name","CSS");

        Element score = DocumentHelper.createElement("score");
        score.addAttribute("name","90");

        student.add(name);
        student.add(course);
        student.add(score);

        rootElement.add(student);

        XMLWriter writer = new HTMLWriter(new FileWriter("src/jianda/Jianda1.xml"));
        writer.write(doc);
        System.out.println("新增成功");
        writer.close();
    }
    public static void update() throws IOException {
        Document doc = null;
        try{
            SAXReader saxReader = new SAXReader();
            doc = saxReader.read("src/jianda/Jianda1.xml");
        }catch (Exception e){
            e.printStackTrace();
        }
        // 修改节点属性
        Element root = doc.getRootElement();
        List student = root.elements("student");
        for (Iterator it = student.listIterator(); it.hasNext();) {  //遍历修改
            Element element = (Element) it.next();
            for (Iterator itscore = element.elementIterator();itscore.hasNext();){
                Element score = (Element) itscore.next();
                if (element.attributeValue("id").equals("2")){
                    if (score.getName().equals("score")){
                        if (score.attributeValue("name").equals("85")){
                            score.addAttribute("name","60");
                            System.out.println("修改成功");
                        }
                    }
                }
            }
        }
        XMLWriter writer = new HTMLWriter(new FileWriter("src/jianda/Jianda1.xml"));
        writer.write(doc);

        writer.close();
    }
    public static void delete() throws IOException {
        Document doc = null;
        try {
            SAXReader saxReader = new SAXReader();
            doc = saxReader.read("src/jianda/Jianda1.xml");
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 删除节点
        Element root = doc.getRootElement();
        List student = root.elements("student");
        for (Iterator it = student.listIterator(); it.hasNext(); ) {
            Element element = (Element) it.next();
            if (element.getName().equals("student")) {
                if (element.attributeValue("id").equals("1")){
                    Element parentElm = element.getParent();
                    parentElm.remove(element);
                    System.out.println("删除成功");
                }
            }
        }
        XMLWriter writer = new HTMLWriter(new FileWriter("src/jianda/Jianda1.xml"));
        writer.write(doc);
        writer.close();
    }
}
